<?php
  final class Utility
  {
    public static function getVars($obj)
    {
      return get_class_vars(get_class($obj));
    }
    
    public static function getMethods($obj)
    {
      return get_class_methods(get_class($obj));
    }
    
    public static function convertObjectVarsToArray($object)
    {
      if(!is_object($object))
        return false;

      return get_object_vars($object);
    }
    
    public static function isFunctionInObject($obj, $function)
    {
      return method_exists($obj, $function);
    }

    public static function escapeString($str)
    {
    	if ($str !== null)
      {
    		$str = str_replace(array('\\','\''),array('\\\\','\\\''),$str);
    		$str = "'".$str."'";
    	} else
      {
    		$str = "null";
    	}
    	return $str;
    }
    
    public static function isLastIndex($item, $array )
    {
       if( $array[count($array)-1] === $item)
          return true;

        return false;
    }
    
    public static function prepareArrayItem(&$array, $key, $value, $attributes)
    {
      $array[$key] = array('value'=>$value, 'attributes'=>$attributes);
      return $array;
    }

    public static function getContentTypeByExtention($extention)
    {
      $contentType = 'application/force-download';
      switch( $extention )
      {
        case "pdf": $contentType="application/pdf"; break;
        case "exe": $contentType="application/octet-stream"; break;
        case "zip": $contentType="application/zip"; break;
        case "doc": $contentType="application/msword"; break;
        case "xls": $contentType="application/vnd.ms-excel"; break;
        case "ppt": $contentType="application/vnd.ms-powerpoint"; break;
        case "gif": $contentType="image/gif"; break;
        case "png": $contentType="image/png"; break;
        case "jpeg":
        case "jpg": $contentType="image/jpg"; break;
        default: $contentType="application/force-download";
      }

      return $contentType;
    }

    public static function getExtention($filePath)
    {
      $segments = explode('.', $filePath);
      $extention = strtolower(trim($segments[count($segments)-1]));

      return $extention;
    }

    public function getContentTypeByFilePath($filePath)
    {
      $extention = self::getExtention($filePath);
      return self::getContentTypeByExtention($extention);
    }
    
    public static function forceDownload($filePath, $contentType = '')
    {
      if($contentType == '')
        $contentType = self::getContentTypeByFilePath($filePath);
      
      header('Pragma: public');   // required
      header('Expires: 0');       // no cache
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Cache-Control: private',false);
      header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
      header('Content-Transfer-Encoding: binary');
      header('Content-Length: '.filesize($filePath));    // provide file size
      header('Content-Type: '.$contentType);
      readfile($filePath);       // push it out
      exit;
    }

    /**
     * @return void
    */
    public static function forceDownloadData($data, $fileName = 'default', $contentType='application/force-download')
    {      
      header('Pragma: public');   // required
      header('Expires: 0');       // no cache
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Cache-Control: private',false);
      header('Content-Disposition: attachment; filename="'.$fileName.'"');
      header('Content-Transfer-Encoding: binary');
      header("Content-Type: $contentType");
      echo $data;       // push it out
      exit;
    }

    public static function generatePassword($length=9, $strength=0) {
            $vowels = 'aeuy';
            $consonants = 'bdghjmnpqrstvz';
            if ($strength & 1) {
                    $consonants .= 'BDGHJLMNPQRSTVWXZ';
            }
            if ($strength & 2) {
                    $vowels .= "AEUY";
            }
            if ($strength & 4) {
                    $consonants .= '23456789';
            }
            if ($strength & 8) {
                    $consonants .= '@#$%';
            }

            $password = '';
            $alt = time() % 2;
            for ($i = 0; $i < $length; $i++) {
                    if ($alt == 1) {
                            $password .= $consonants[(rand() % strlen($consonants))];
                            $alt = 0;
                    } else {
                            $password .= $vowels[(rand() % strlen($vowels))];
                            $alt = 1;
                    }
            }
            return $password;
    }
    
    public static function jsMessage($message,$url='',$confirm=false){ 
        $ret = false;
        if($url==''){
            $res=Utility::addScript("alert('".$message."');");
        }
        elseif($url!=''){
            $res=Utility::addScript("alert('".$message."');top.location=\"".$url."\";");
        }
        if ($confirm)
        {
            $s = "var s=confirm('".$message."');if (s==true){ ".$ret=true."return s}";
        }
        echo $res;
        return $ret;
        exit();
    }
    
    public static function addScript($scipt)
    {
        $res = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
        $res = $res."<title></title></head><body>"; 
        $res = $res."<script type=\"text/JavaScript\">".$scipt."<\/script>";
        $res =$res."</body></html>";
        return $res ;
    }

  }
?>
